import 'package:fish_redux/fish_redux.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/attendanceRecord/entity/attendanceRecordEntity.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/studentsReview/entity/studentReviewEntity.dart';
import 'package:school_safe_teacher/utils/sp_utils.dart';

import 'action.dart';
import 'state.dart';

Effect<attendanceRecordState> buildEffect() {
  return combineEffects(<Object, Effect<attendanceRecordState>>{
    Lifecycle.initState: _init,
    attendanceRecordAction.onRefresh:_onRefresh,

  });
}

void _init(Action action, Context<attendanceRecordState> ctx){
  _onRefresh(action, ctx);
}

void _onRefresh(Action action, Context<attendanceRecordState> ctx) {
  _request(ctx, 1);
}
Future<void> _request(Context<attendanceRecordState> ctx, int page) async {
  Map<String, String> params = Map();
  params["page_size"] = Constants.PAGE_SIZE.toString();
  params["page"] = page.toString();
  params["class_id"] = await SpUtil.getStringItem(Constants.classID);
  DioApi.instance.asyncRequestNetwork<attendanceRecordEntity>(
      Method.post, HttpApi.GETAttendanceRecord, ctx.context,params: params,
      onStart: () {},
      onSuccessList: (List<dynamic> list) async {
        List<attendanceRecordEntity> newsList = [];
        list.forEach((element) {
          newsList.add(EntityFactory.generateOBJ(element));
        });
        ctx.dispatch(attendanceRecordActionCreator.onSuccess(newsList));
      },
      onError: (String msg) {
        showToast(msg);
        ctx.state.easyRefreshController.finishRefresh();
        ctx.state.easyRefreshController.finishLoad();
      },
      onDone: () {
        ctx.dispatch(attendanceRecordActionCreator.onComplete());
      },
      isList: true);
}
